/** 
 * Servlet
 *  - prevod mezi komunikaci mezi MIDletem a databazi
 *  - v soucansne dobe komunikace s DB na localhostu
 *  - zatim pouze vykonani predem naprogramovaneho prikazu, casem predelat na dynamicky
 *  - zpetne poslani prectenych argumetnu
 *  - nacitani 3 poslanych parametru - zatim pouze 3 - pozdeji predelat na metodu doPost (bezpecnejsi)
 */
package testPackage;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;


import java.sql.*;
/**
 * 
 * @author Tomas Cinert
 * @version 0.1
 * 
 *
 */

@WebServlet("/test2")		
public class TestServlet extends HttpServlet {
	Connection con;
	String message="jdbc:mysql://localhost:3306/skola"+","+"A"+","+"aaa";		// Spojeni
	Statement stmt = null;
   // ResultSet rs = null;
    PrintWriter out;
    String vypis=null;
    String a=null;
    Statement st=null;
    int param1;
    int param2;
    int param3;
    public void init() {
    }
    /**
     * Metoda na zpracovani paramateru
     *  - zatim pouze spousteni metody connect() a poslani potvrzeni zpet
     *  
     */
    public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, 
            IOException {
    		
            
            vypis=connect();
            message += "100 ok";		// Testovaci 
        
        response.setContentType("text/plain");
        response.setContentLength(message.length());
        PrintWriter out = response.getWriter();
        out.println(vypis);					//testovaci vypisy
       // System.out.println(vypis);    
       // System.out.println(param3);
       uzavreni();
    /*    //in.close();
        out.close();
        out.flush();
        */
    }
    /**
     * Metoda na uzavreni spojeni
     */
    public void uzavreni()
    {
         try {
		//	con.close();
			out.close();
	        out.flush();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
    }
    /**
     * Metoda na zpracovani prijatych parametru
     * - zatim prijem pouze 3 parametru pomoci metody doGet
     * - pozdeji predelat na metodu doPost
     */
    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, 
            IOException {
    	param1=Integer.parseInt(request.getParameter("param1"));		// Nacteni parametru poslanych ze zarizeni
    	param2=Integer.parseInt(request.getParameter("param2"));
    	param3=Integer.parseInt(request.getParameter("param3"));
        doPost(request,response);
    }
    /**
     * Metoda k vztvoreni zapisu 
     *- pouze jako ochranna, pokud by doslo k vymazani potrebne tabulky
     */
    private void vytvoritDB()
    {
    	try {
    		st.executeUpdate("CREATE TABLE result (Prvni int, Druhy int, Treti int)");
    	}catch(SQLException e){
    		
    	}
    }
    /**
     * Metoda na pripojeni k DB
     * - pripojeni k DB
     * - vlozeni do predem definovane tabulky prijate parametry
     * - osetreni vyjimek
     */
    private String connect()   {
    	ResultSet rs=null;
    	Connection conn=null;
        try{
    		Class.forName("com.mysql.jdbc.Driver").newInstance();		// Nacteni ovladace pro praci s DB
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/skola","A","aaa");
            			// Pripojeni na localhost
            st= conn.createStatement();
            st.executeUpdate("INSERT INTO result (Prvni, Druhy, Treti, Ctvrty) VALUES ("+param1+","+param2+","+param3+",4)");		// Prikaz k "vytvoreni"
	        st.close();
	        conn.close();
    			
    	}catch (SQLException e){			// SQL exception, pokud dana tabulka neexistuje
    		String exc = e.getSQLState();
    		if (exc.equalsIgnoreCase("42S02"))		// SQL chyba pokud neni nalezena tabulka
    		{
    			//st.executeUpdate("");
    			return "tabulka neni";
    		}else									// Spojeni nebylo navazano
    			return e.getMessage();
    		
    		//return e.getSQLState();
    	}catch (Exception e){
    		return e.toString();
    	}
        
        return "OK";		//testovaci vypis
    	 
  
    }
}